package com.bilu.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.bilu.common.utility.RequestUtility;
import com.bilu.common.utility.ResponseUtility;
import com.bilu.service.BiLuService;
import com.bilu.service.TableService;

@Controller
public class BiLuController {
	/**
	 * http://localhost:8080/bilu/service.do?templateId=table
	 * http://localhost:8080/bilu/service.do?templateId=bilu
	 * @param request
	 * @param response
	 */
    @RequestMapping(value = "/bilu/service.do", method = RequestMethod.POST)
    public void doPost(HttpServletRequest request, HttpServletResponse response){
    	String params = RequestUtility.readRequest(request);
    	JSONObject result = new JSONObject();
    	if(params == null || params.trim().length() == 0) {
    		result.put("code", 1);
    		result.put("message", "请求参数为空");
    		ResponseUtility.response(response, result.toString());
    		return;
    	}
    	
    	JSONObject json = JSONObject.parseObject(params);
    	if(json == null) {
    		result.put("code", 1);
    		result.put("message", "请求参数不是JSON格式");
    		ResponseUtility.response(response, result.toString());
    		return;
    	}
    	
    	//用来判断是使用哪个模板
    	String templateId = json.getString("templateId");
    	try {
    		if("table".equals(templateId)) {
        		TableService.table(json, response);
        	}else if("bilu".equals(templateId)) {
        		BiLuService.bilu(json, response);
        	}
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	
    	result.put("code", 1);
		result.put("message", "系统异常");
		ResponseUtility.response(response, result.toString());
    }
}
